/**
 * Copyright 2007-2010 非也
 * All rights reserved. 
 * 
 * This library is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License v3 as published by the Free Software
 * Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with this library; if not, see http://www.gnu.org/licenses/lgpl.html.
 *
 */
package org.fireflow2.designer.eclipse.modelwrapper.basic;

import java.util.List;

import org.fireflow.pdl.fpdl20.diagram.Diagram;
import org.fireflow.pdl.fpdl20.diagram.GroupShape;
import org.fireflow.pdl.fpdl20.diagram.LaneShape;
import org.fireflow.pdl.fpdl20.diagram.PoolShape;
import org.fireflow2.designer.eclipse.modelwrapper.NodeWrapper;
import org.fireflow2.designer.eclipse.modelwrapper.SubProcessWrapper;
import org.fireflow2.designer.eclipse.modelwrapper.Wrapper;

/**
 *
 * @author 非也 nychen2000@163.com
 * Fire Workflow 官方网站：www.firesoa.com 或者 www.fireflow.org
 *
 */
public class PoolWrapper extends NodeWrapper {

	public PoolWrapper(SubProcessWrapper parent,PoolShape poolShape){
		super(parent,null,poolShape);
		initNodes();
	}
	
	public boolean isHorizonal(){
		SubProcessWrapper subflowWrapper = (SubProcessWrapper)this.getParent();
		Diagram diagram = (Diagram)subflowWrapper.getDiagramElement();
		if (Diagram.HORIZONAL.equals(diagram.getDirection())){
			return true;
		}
		return false;
	}
	
	/* 所有的connector 放在diagram里面 2012-05-11
	public void initConnectors(){
		PoolShape poolShape = (PoolShape)this.getDiagramElement();
		ModelWrapper parent = (ModelWrapper)this.getParent();
		SubProcessWrapper parentWrapper = null;
		if (parent instanceof SubProcessWrapper){
			parentWrapper = (SubProcessWrapper)parent;
		}
		if (parentWrapper==null) return;//模型不正确，无法初始化
		
		List<TransitionShape> transitionShapeList = poolShape.getTransitions();
		if (transitionShapeList!=null){
			for (TransitionShape transShape : transitionShapeList){
				Transition trans = subflow.getTransition(transShape.getWorkflowElementRef());
				TransitionWrapper transWrapper = new TransitionWrapper(trans,transShape);
				
				String fromNodeSN = transShape.getFromWorkflowNodeShape()==null?"":transShape.getFromWorkflowNodeShape().getSn();
				String toNodeSN = transShape.getToWorkflowNodeShape()==null?"":transShape.getToWorkflowNodeShape().getSn();
				NodeWrapper source = (NodeWrapper)parentWrapper.sn_modelwrapper_map.get(fromNodeSN);
				NodeWrapper target = (NodeWrapper)parentWrapper.sn_modelwrapper_map.get(toNodeSN);
				if (source!=null) {
					source.setLeavingTransition(transWrapper);
					transWrapper.setSource(source);
				}
				if (target!=null){
					target.setEnteringTransition(transWrapper);
					transWrapper.setTarget(target);
				}

				transWrapper.setParent(this);	
				allChildren.add(transWrapper);
				parentWrapper.sn_modelwrapper_map.put(transWrapper.getSn(), transWrapper);				
				parentWrapper.transitionsWrapper.set(transWrapper);
			}
		}
		
		for (Wrapper wrapper : this.allChildren){
			if (wrapper instanceof GroupWrapper){
				((GroupWrapper)wrapper).initConnectors();
			}
		}
	}
	*/
	protected void initNodes(){
		PoolShape poolShape = (PoolShape)this.getDiagramElement();
		List<LaneShape> laneList = poolShape.getLanes();
		if (laneList!=null){
			for (LaneShape lane : laneList){
				LaneWrapper laneWrapper = new LaneWrapper(subProcessWrapper,lane);

				laneWrapper.setParent(this);
				this.getChildren().add(laneWrapper);
				subProcessWrapper.putToSnModelWrapperMap(laneWrapper.getSn(), laneWrapper);					

			}
		}

	}
	

	
	public void addChild(Wrapper child,int index){
		PoolShape poolShape = (PoolShape)this.getDiagramElement();
		LaneShape elm = (LaneShape)((LaneWrapper)child).getDiagramElement();
		poolShape.addLane(elm);

		subProcessWrapper.putToSnModelWrapperMap(child.getSn(), child);

		super.addChild(child,index);
	}
	
	public int deleteChild(Wrapper child){
		child.setParent(null);
		
		PoolShape poolShape = (PoolShape)this.getDiagramElement();
		LaneShape elm = (LaneShape)((LaneWrapper)child).getDiagramElement();
		poolShape.getLanes().remove(elm);
		
		subProcessWrapper.removeFromSnModelWrapperMap(child.getSn());
		
		return super.deleteChild(child);
	}
}
